home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / reset tools / resethandler / source / resethandler.e next >
Text File  |  1996-04-07  |  6KB  |  217 lines

  1. /* ResetHandler.e
  2.  *
  3.  * Warns the user if a keyboard reset has been triggered and allows the execution
  4.  * of user-defined DOS commands at this point.
  5.  *
  6.  * © FtC of EfS 131195
  7.  *
  8.  * v1.01: 151295
  9.  *
  10.  * Changed the default behaviour to not display the (not really useful) requester
  11.  * and to execute s:ShutDown.sequence instead.
  12.  *
  13.  * Changed the option "NoWarn" into "Warn"
  14.  *
  15.  * Cleaned up the docs
  16.  *
  17.  */
  18.  
  19. OPT PREPROCESS
  20.  
  21. MODULE 'dos/dos','utility/tagitem','dos/rdargs','dos/dostags','devices/keyboard',
  22.        'commodities','libraries/commodities','exec/ports','exec/tasks','exec/io',
  23.        'exec/interrupts','exec/nodes','intuition/intuition','workbench/startup',
  24.        'icon','workbench/workbench'
  25.  
  26. ENUM OK=0,RDA,BROKER,NOSIG
  27.  
  28. OBJECT argvec
  29.     shutdowncmd : PTR TO CHAR
  30.     wait    : PTR TO LONG
  31.     warn
  32. ENDOBJECT
  33.  
  34. OBJECT asmdata
  35.     task    : PTR TO tc
  36.     signal
  37. ENDOBJECT
  38.  
  39. RAISE RDA    IF ReadArgs()=NIL,
  40.       "LIB"  IF OpenLibrary()=NIL,
  41.       "DEV"  IF OpenDevice()<>NIL,
  42.       "MEM"  IF CreateMsgPort()=NIL,
  43.       BROKER IF CxBroker()=NIL
  44.  
  45. PROC main() HANDLE
  46.     DEF rda=0        : PTR TO rdargs,
  47.     av        : argvec,
  48.     myname[33]    : ARRAY OF CHAR,
  49.     nb        : PTR TO newbroker,
  50.     broker=0    : PTR TO CHAR,        -> well, it isn't, but it's considered a black box...
  51.     cxport=0    : PTR TO mp,
  52.     keyport=0    : PTR TO mp,
  53.     keyreq=0    : PTR TO iostd,
  54.     int=0        : PTR TO is,
  55.     data=0        : PTR TO asmdata,
  56.     signal=0,installed=0,
  57.     msg        : PTR TO CHAR,        -> >same<
  58.     rhd        : PTR TO CHAR,
  59.     dobj=0        : PTR TO diskobject,
  60.     wb        : PTR TO wbstartup,
  61.     msgid, msgtype, running=TRUE, cxmask, recd, listen=TRUE, oldcd
  62.  
  63.     cxbase := 0
  64.     iconbase := 0
  65.     rhd := 'ResetHandler'
  66.     IF GetProgramName(myname, 32) = 0 THEN myname := rhd
  67.  
  68.     cxbase := OpenLibrary('commodities.library', 37)
  69.     iconbase := OpenLibrary('icon.library', 33)
  70.  
  71.     av.shutdowncmd := 'Execute S:Shutdown-Sequence'
  72.     av.wait       := [0]
  73.     av.warn       := FALSE
  74.  
  75.     IF wbmessage
  76.     wb := wbmessage
  77.     oldcd := CurrentDir(wb.arglist.lock)
  78.     dobj := GetDiskObject(wb.arglist.name)
  79.     IF dobj
  80.         recd := FindToolType(dobj.tooltypes, 'SHUTDOWNCOMMAND')
  81.         IF recd
  82.         av.shutdowncmd := New(StrLen(recd) + 1)
  83.         AstrCopy(av.shutdowncmd, recd)
  84.         ENDIF
  85.         recd := FindToolType(dobj.tooltypes, 'WAIT')
  86.         IF recd THEN av.wait := NEW [Val(recd)]
  87.         recd := FindToolType(dobj.tooltypes, 'WARN')
  88.         IF recd THEN av.warn := TRUE
  89.         FreeDiskObject(dobj)
  90.     ENDIF
  91.     CurrentDir(oldcd)
  92.     ELSE
  93.     rda := ReadArgs('ShutDownCommand,Wait/N,Warn/S', av, NIL)
  94.     ENDIF
  95.     IF av.shutdowncmd[] = 0 THEN av.shutdowncmd := NIL
  96.  
  97.     cxport := CreateMsgPort()
  98.     nb := [NB_VERSION, 0, rhd, 'ResetHandler 1.01 by FtC of EfS', 'Warns the user in case of a reset', NBU_UNIQUE OR NBU_NOTIFY, 0, 0, 0, cxport, 0]:newbroker
  99.     broker := CxBroker(nb, NIL)
  100.     ActivateCxObj(broker, 1)
  101.  
  102.     signal := AllocSignal(-1)
  103.     IF signal = -1 THEN Raise(NOSIG)
  104.     NEW data
  105.     data.task    := FindTask(NIL)
  106.     data.signal := Shl(1, signal)
  107.  
  108.     keyport := CreateMsgPort()
  109.     keyreq  := CreateIORequest(keyport, SIZEOF iostd)
  110.     OpenDevice('keyboard.device', NIL, keyreq, NIL)
  111.  
  112.     NEW int
  113.     int.ln.pri       := 32
  114.     int.ln.name    := rhd
  115.     int.code       := {handlercode}
  116.     int.data       := data
  117.     keyreq.data    := int
  118.     keyreq.command := KBD_ADDRESETHANDLER
  119.     DoIO(keyreq)
  120.     installed := TRUE
  121.  
  122.     cxmask    := Shl(1, cxport.sigbit)
  123.     WHILE running
  124.     recd := Wait(data.signal OR cxmask OR SIGBREAKF_CTRL_C OR SIGBREAKF_CTRL_D)
  125.     IF recd AND data.signal
  126.         IF listen
  127.         IF av.shutdowncmd[]
  128.             SystemTagList(av.shutdowncmd, [SYS_USERSHELL, TRUE, TAG_END])
  129.             IF av.wait[] THEN Delay(av.wait[] * 50)
  130.         ENDIF
  131.         IF av.warn THEN req(rhd, 'Warning: Your Amiga will\nreset in a few seconds!', 'Reset NOW')
  132.         ENDIF
  133.         keyreq.command := KBD_RESETHANDLERDONE
  134.         DoIO(keyreq)    -> Good-bye, old world, so long, farewell... it's been enough, see you in hell :-)
  135.         Wait(0)        -> just in case...
  136.     ENDIF
  137.     IF recd AND cxmask
  138.         WHILE msg := GetMsg(cxport)
  139.         msgid := CxMsgID(msg)
  140.         msgtype := CxMsgType(msg)
  141.         ReplyMsg(msg)
  142.         IF msgtype = CXM_COMMAND
  143.             SELECT msgid
  144.             CASE CXCMD_DISABLE
  145.                 ActivateCxObj(broker, 0)
  146.                 listen := FALSE
  147.             CASE CXCMD_ENABLE
  148.                 ActivateCxObj(broker, 1)
  149.                 listen := TRUE
  150.             CASE CXCMD_KILL
  151.                 running := FALSE
  152.             CASE CXCMD_UNIQUE
  153.                 running := FALSE    -> start it again to remove it
  154.             ENDSELECT
  155.         ENDIF
  156.         ENDWHILE
  157.     ENDIF
  158.     IF recd AND (SIGBREAKF_CTRL_C OR SIGBREAKF_CTRL_D) THEN running := FALSE
  159.  
  160.     ENDWHILE
  161.  
  162.     Raise(OK)
  163. handlercode:            -> a1 points to data
  164.     MOVE.L  4(A1),D0        -> get data.signal
  165.     MOVE.L  (A1),A1        -> get data.task
  166.     MOVE.L  A6,-(A7)        -> save A6
  167.     MOVE.L  $4.W,A6        -> get SysBase
  168.     JSR     Signal(A6)        -> signal!
  169.     MOVE.L  (A7)+,A6        -> restore A6
  170. /*    MOVE.B  $bfe001,D1
  171.     BCHG    #1,D1        -> optionally blink the power LED
  172.     MOVE.B  D1,$bfe001 */
  173.     RTS
  174. EXCEPT
  175.     SELECT exception
  176.     CASE RDA
  177.         PrintFault(IoErr(), myname)
  178.         PutStr('$VER: ResetHandler 1.01 (15.12.95)\r\nby FtC of EfS\n')
  179.     CASE "MEM"
  180.         PutStr('Out of memory!\n')
  181.     CASE "LIB"
  182.         PutStr('Couldn''t open standard libraries... strange\n')
  183.     CASE "DEV"
  184.         PutStr('Couldn''t open keyboard.device!\n')
  185.     CASE NOSIG
  186.         PutStr('Couldn''t alloc a signal bit!\n')
  187.     ENDSELECT
  188.     IF rda        THEN FreeArgs(rda)
  189.     IF installed
  190.     keyreq.data    := int
  191.     keyreq.command := KBD_REMRESETHANDLER
  192.     DoIO(keyreq)
  193.     ENDIF
  194.     IF cxport        THEN DeleteMsgPort(cxport)
  195.     IF broker        THEN DeleteCxObj(broker)
  196.     IF keyreq
  197.     IF keyreq.device THEN CloseDevice(keyreq)
  198.     DeleteIORequest(keyreq)
  199.     ENDIF
  200.     IF keyport        THEN DeleteMsgPort(keyport)
  201.     IF cxbase        THEN CloseLibrary(cxbase)
  202.     IF iconbase     THEN CloseLibrary(iconbase)
  203.     IF signal        THEN FreeSignal(signal)
  204. ENDPROC IF exception THEN 20 ELSE 0
  205.  
  206. PROC req(title : PTR TO CHAR, body : PTR TO CHAR, gadgets : PTR TO CHAR)
  207.     DEF easy : easystruct
  208.  
  209.     easy.structsize := SIZEOF easystruct
  210.     easy.flags := 0
  211.     easy.title := title
  212.     easy.textformat := body
  213.     easy.gadgetformat := gadgets
  214.  
  215.     EasyRequestArgs(NIL, easy, NIL, NIL)
  216. ENDPROC
  217.